$(document).ready(function() {
	
	$('.styleswitch').click(function(){
		switchStylestyle(this.getAttribute("rel"));
		return false;
	});
	var c = readCookie('style');
	if (c) switchStylestyle(c);
});

//下面用于图片上传预览功能
function setImagePreview(avalue) {
    var docObj=document.getElementById("pic");

    var imgObjPreview=document.getElementById("preview");
    if(docObj.files &&docObj.files[0]){
        //火狐下，直接设img属性
        imgObjPreview.style.display = 'block';
        imgObjPreview.style.width = '150px';
        imgObjPreview.style.height = '180px';
        //imgObjPreview.src = docObj.files[0].getAsDataURL();

        //火狐7以上版本不能用上面的getAsDataURL()方式获取，需要一下方式
        imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
    }else{
        //IE下，使用滤镜
        docObj.select();
        var imgSrc = document.selection.createRange().text;
        var localImagId = document.getElementById("localImag");
        //必须设置初始大小
        localImagId.style.width = "150px";
        localImagId.style.height = "180px";
        //图片异常的捕捉，防止用户修改后缀来伪造图片
        try{
            localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
        }catch(e){
            alert("您上传的图片格式不正确，请重新选择!");
            return false;
        }
        imgObjPreview.style.display = 'none';
        document.selection.empty();
    }
    return true;
}

//设置主题
function switchStylestyle(styleName){
	$('link[rel=stylesheet][title]').each(function(i){
		this.disabled = true;
		if (this.getAttribute('title') == styleName) 
			this.disabled = false;
	});
	
	$("iframe").contents().find('link[rel=stylesheet][title]').each(function(i) {
		this.disabled = true;
		if (this.getAttribute('title') == styleName) {
			this.disabled = false;
		}
	});
	createCookie('style', styleName, 365);
}
//cookie
function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name){
	createCookie(name,"",-1);
}
// /cookie functions




//退出
function logout(){
	$.messager.confirm('提示','您确认想要退出吗？',function(r){    
	    if (r){    
	    	window.location.href='logout';
	    }    
	});
}

function randomString(len) {
    len = len || 32;
    var $chars = '12345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    var maxPos = $chars.length;
    var pwd = '';
    for (i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

//创建tabs
function addTab(title, url, icon) {
	var content = '<iframe src="' + url + '" frameborder="0" style="border:0;width:100%;height:99%;"></iframe>';
	var t;
	if(window.parent) {
		t = window.parent.$('#index_tabs');
	} else {
		t = $('#index_tabs');
	}
	var opts = {
		title : title,
		closable : true,
		iconCls : icon,
		content : content,
		border : false,
		fit : true
	};
	if (t.tabs('exists', opts.title)) {
		t.tabs('select', opts.title);
		t.tabs('update',{
			tab: t.tabs('getSelected'),
			options: {
				content : content
			}
		});
	} else {
		var tabsCount = t.tabs('tabs').length; 
		if(tabsCount > 10) {
			$.messager.confirm("系统提示", '您当前打开了太多的页面，如果继续打开，会造成程序运行缓慢，无法流畅操作！', function (b) {                
				if (b) {
					t.tabs('add', opts);
				}
			});
		} else {
			t.tabs('add', opts);
		}
	}
}

//刷新tabs
function refreshTab(url) {
	var content = '<iframe src="' + url + '" frameborder="0" style="border:0;width:100%;height:98%;"></iframe>';
	return content;
}

//关闭tabs
function closeTab() {
	if(window.parent) {
		t = window.parent.$('#index_tabs');
		var tab = t.tabs('getSelected');
		var index = t.tabs('getTabIndex', tab);
		t.tabs('close', index);
	}
}

/**
 * 增加formatString功能
 * 
 * 使用方法：$.formatString('字符串{0}字符串{1}字符串','第一个变量','第二个变量');
 */
$.formatString = function(str) {
	for ( var i = 0; i < arguments.length - 1; i++) {
		str = str.replace("{" + i + "}", arguments[i + 1]);
	}
	return str;
};

function getMap(){//初始化map_，给map_对象增加方法，使map_像个Map
    var map_=new Object();
    //属性加个特殊字符，以区别方法名，统一加下划线_
    map_.put=function(key,value){    map_[key]=value;}
    map_.get=function(key){    return map_[key];}
    map_.remove=function(key){    delete map_[key];}
    map_.keyset=function(){
        var ret="";
        for(var p in map_){
            if(typeof p =='string' && p.substring(p.length-1)=="_"){
                ret+=",";
                ret+=p;
            }
        }
        if(ret==""){
            return ret.split(","); //empty array
        }else{
            return ret.substring(1).split(",");
        }
    }
    return map_;
}


function submitForm() {
	
}